package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.CheckinAssessClassDTO1;
import com.api.shopapi.entity.dto.ElderlyCheckinAssessDTO;
import com.api.shopapi.entity.dto.ElderlyCheckinAssessWaitDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 待评估
 */

@RestController
@RequestMapping("ElderlyCheckinAssessWait")
public class ElderlyCheckinAssessWaitController extends BaseController{


    @Autowired
    @Qualifier("ElderlyCheckinAssessWaitServiceImplOne")
    private ElderlyCheckinAssessWaitService elderlyCheckinAssessWaitService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;



    //gov
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            //String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/ElderlyCheckinAssessWait/GovPage");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<ElderlyCheckinAssessWait> pageInfo = elderlyCheckinAssessWaitService.pageByGovCode(pageIndex, pageSize, staff.getGovernmentCode());

            List<ElderlyCheckinAssessWaitDTO> listDTO = new ArrayList<>();
            for(ElderlyCheckinAssessWait elderlyCheckinAssessWait:pageInfo.getList()){
                ElderlyCheckinAssessWaitDTO elderlyCheckinAssessWaitDTO = mapper.map(elderlyCheckinAssessWait,ElderlyCheckinAssessWaitDTO.class);
                Elderly elderly = elderlyService.getByElderlyCode(elderlyCheckinAssessWait.getElderlyCode());
                elderlyCheckinAssessWaitDTO.setElderlyName(elderly.getName());
                listDTO.add(elderlyCheckinAssessWaitDTO);
            }
            pageInfo.setList(Collections.unmodifiableList(listDTO));

            return success(pageInfo);
        }catch (Exception e) {
            insertError("/ElderlyCheckinAssessWait/GovPage", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //ins
    @RequestMapping(value = "InsPage", method = RequestMethod.POST)
    public MyResult InsPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            //String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/ElderlyCheckinAssessWait/InsPage");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<ElderlyCheckinAssessWait> pageInfo = elderlyCheckinAssessWaitService.pageByInsCode(pageIndex, pageSize, institutionCode);

            List<ElderlyCheckinAssessWaitDTO> listDTO = new ArrayList<>();
            for(ElderlyCheckinAssessWait elderlyCheckinAssessWait:pageInfo.getList()){
                ElderlyCheckinAssessWaitDTO elderlyCheckinAssessWaitDTO = mapper.map(elderlyCheckinAssessWait,ElderlyCheckinAssessWaitDTO.class);
                Elderly elderly = elderlyService.getByElderlyCode(elderlyCheckinAssessWait.getElderlyCode());
                elderlyCheckinAssessWaitDTO.setElderlyName(elderly.getName());
                listDTO.add(elderlyCheckinAssessWaitDTO);
            }
            pageInfo.setList(Collections.unmodifiableList(listDTO));

            return success(pageInfo);
        }catch (Exception e) {
            insertError("/ElderlyCheckinAssessWait/InsPage", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //ins
    @RequestMapping(value = "InsPageApp", method = RequestMethod.POST)
    public MyResult InsPageApp(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            //String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyCheckinAssessWait/InsPageApp");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);
            String institutionCode = user.getInstitutionCode();

            PageInfo<ElderlyCheckinAssessWait> pageInfo = elderlyCheckinAssessWaitService.pageByInsCode(pageIndex, pageSize, institutionCode);

            List<ElderlyCheckinAssessWaitDTO> listDTO = new ArrayList<>();
            for(ElderlyCheckinAssessWait elderlyCheckinAssessWait:pageInfo.getList()){
                ElderlyCheckinAssessWaitDTO elderlyCheckinAssessWaitDTO = mapper.map(elderlyCheckinAssessWait,ElderlyCheckinAssessWaitDTO.class);
                Elderly elderly = elderlyService.getByElderlyCode(elderlyCheckinAssessWait.getElderlyCode());
                elderlyCheckinAssessWaitDTO.setElderlyName(elderly.getName());
                listDTO.add(elderlyCheckinAssessWaitDTO);
            }
            pageInfo.setList(Collections.unmodifiableList(listDTO));

            return success(pageInfo);
        }catch (Exception e) {
            insertError("/ElderlyCheckinAssessWait/InsPageApp", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }





}
